function defineProperty(obj, key, value) {
    return key in obj ? Object.defineProperty(obj, key, {
        value: value,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : obj[key] = value, obj;
}

function isDateValid(dateValue) {
    return (0, utils.isDef)(dateValue) && !isNaN(new Date(dateValue).getTime());
}

function clamp(value, minValue, maxValue) {
    return Math.min(Math.max(value, minValue), maxValue);
}

function padWithZeros(value) {
    return ("00" + value).slice(-2);
}

function createArray(length, valueFunction) {
    for (var array = -1, resultArray = Array(length < 0 ? 0 : length); ++array < length; ) resultArray[array] = valueFunction(array);
    return resultArray;
}

function parseInteger(value) {
    if (value) {
        for (;isNaN(parseInt(value, 10)); ) value = value.slice(1);
        return parseInt(value, 10);
    }
}

function getDaysInMonth(year, month) {
    return 32 - new Date(year, month - 1, 32).getDate();
}

var destructureArray = function(array, limit) {
    if (Array.isArray(array)) return array;
    if (Symbol.iterator in Object(array)) return function(array, limit) {
        var result = [], done = !0, error = !1, errorValue = void 0;
        try {
            for (var index, iterator = array[Symbol.iterator](); !(done = (index = iterator.next()).done) && (result.push(index.value), 
            !limit || result.length !== limit); done = !0) ;
        } catch (error) {
            error = !0, errorValue = error;
        } finally {
            try {
                !done && iterator.return && iterator.return();
            } finally {
                if (error) throw errorValue;
            }
        }
        return result;
    }(array, limit);
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
}, component = require("../common/component"), utils = require("../common/utils"), pickerShared = require("../picker/shared"), currentYear = new Date().getFullYear(), defaultFormatter = function(value, type) {
    return type;
};

(0, component.VantComponent)({
    classes: [ "active-class", "toolbar-class", "column-class" ],
    props: Object.assign({}, pickerShared.pickerProps, {
        formatter: {
            type: Function,
            value: defaultFormatter
        },
        value: null,
        type: {
            type: String,
            value: "datetime"
        },
        showToolbar: {
            type: Boolean,
            value: !0
        },
        minDate: {
            type: Number,
            value: new Date(currentYear - 10, 0, 1).getTime()
        },
        maxDate: {
            type: Number,
            value: new Date(currentYear + 10, 11, 31).getTime()
        },
        minHour: {
            type: Number,
            value: 0
        },
        maxHour: {
            type: Number,
            value: 23
        },
        minMinute: {
            type: Number,
            value: 0
        },
        maxMinute: {
            type: Number,
            value: 59
        }
    }),
    data: {
        innerValue: Date.now(),
        columns: []
    },
    watch: {
        value: function(newValue) {
            var self = this, data = this.data;
            (newValue = this.correctValue(newValue)) === data.innerValue || this.updateColumnValue(newValue).then(function() {
                self.$emit("input", newValue);
            });
        },
        type: "updateColumns",
        minHour: "updateColumns",
        maxHour: "updateColumns",
        minMinute: "updateColumns",
        maxMinute: "updateColumns"
    },
    methods: {
        getPicker: function() {
            if (null == this.picker) {
                var pickerComponent = this.picker = this.selectComponent(".van-datetime-picker"), setColumnValues = pickerComponent.setColumnValues;
                pickerComponent.setColumnValues = function() {
                    for (var argumentsLength = arguments.length, argumentsArray = Array(argumentsLength), index = 0; index < argumentsLength; index++) argumentsArray[index] = arguments[index];
                    return setColumnValues.apply(pickerComponent, [].concat(argumentsArray, [ !1 ]));
                };
            }
            return this.picker;
        },
        updateColumns: function() {
            var formatter = this.data.formatter, formatFunction = void 0 === formatter ? defaultFormatter : formatter, ranges = this.getRanges().map(function(rangeInfo, index) {
                var rangeType = rangeInfo.type, range = rangeInfo.range;
                return {
                    values: createArray(range[1] - range[0] + 1, function(index) {
                        var value = range[0] + index;
                        return value = "year" === rangeType ? "" + value : padWithZeros(value), formatFunction(rangeType, value);
                    })
                };
            });
            return this.set({
                columns: ranges
            });
        },
        getRanges: function() {
            var data = this.data;
            if ("time" === data.type) return [ {
                type: "hour",
                range: [ data.minHour, data.maxHour ]
            }, {
                type: "minute",
                range: [ data.minMinute, data.maxMinute ]
            } ];
            var maxBoundary = this.getBoundary("max", data.innerValue), maxYear = maxBoundary.maxYear, maxDate = maxBoundary.maxDate, maxMonth = maxBoundary.maxMonth, maxHour = maxBoundary.maxHour, maxMinute = maxBoundary.maxMinute, minBoundary = this.getBoundary("min", data.innerValue), minYear = minBoundary.minYear, minDate = minBoundary.minDate, ranges = [ {
                type: "year",
                range: [ minYear, maxYear ]
            }, {
                type: "month",
                range: [ minBoundary.minMonth, maxMonth ]
            }, {
                type: "day",
                range: [ minDate, maxDate ]
            }, {
                type: "hour",
                range: [ minBoundary.minHour, maxHour ]
            }, {
                type: "minute",
                range: [ minBoundary.minMinute, maxMinute ]
            } ];
            return "date" === data.type && ranges.splice(3, 2), "year-month" === data.type && ranges.splice(2, 3), 
            ranges;
        },
        correctValue: function(value) {
            var data = this.data, isDateType = "time" !== data.type;
            if (isDateType && !isDateValid(value) ? value = data.minDate : isDateType || value || (value = padWithZeros(data.minHour) + ":00"), !isDateType) {
                var timeParts = value.split(":"), timeArray = destructureArray(timeParts, 2), hour = timeArray[0], minute = timeArray[1];
                return (hour = padWithZeros(clamp(hour, data.minHour, data.maxHour))) + ":" + (minute = padWithZeros(clamp(minute, data.minMinute, data.maxMinute)));
            }
            return value = Math.max(value, data.minDate), value = Math.min(value, data.maxDate);
        },
        getBoundary: function(type, value) {
            var boundary = {}, date = new Date(value), boundaryDate = new Date(this.data[type + "Date"]), boundaryYear = boundaryDate.getFullYear(), boundaryMonth = 1, boundaryDay = 1, boundaryHour = 0, boundaryMinute = 0;
            return "max" === type && (boundaryMonth = 12, boundaryDay = getDaysInMonth(date.getFullYear(), date.getMonth() + 1), boundaryHour = 23, 
            boundaryMinute = 59), date.getFullYear() === boundaryYear && (boundaryMonth = boundaryDate.getMonth() + 1, date.getMonth() + 1 === boundaryMonth && (boundaryDay = boundaryDate.getDate(), 
            date.getDate() === boundaryDay && (boundaryHour = boundaryDate.getHours(), date.getHours() === boundaryHour && (boundaryMinute = boundaryDate.getMinutes())))), 
            defineProperty(boundary, type + "Year", boundaryYear), defineProperty(boundary, type + "Month", boundaryMonth), defineProperty(boundary, type + "Date", boundaryDay), defineProperty(boundary, type + "Hour", boundaryHour), defineProperty(boundary, type + "Minute", boundaryMinute), boundary;
        },
        onCancel: function() {
            this.$emit("cancel");
        },
        onConfirm: function() {
            this.$emit("confirm", this.data.innerValue);
        },
        onChange: function() {
            var self = this, data = this.data, newValue, picker = this.getPicker();
            if ("time" === data.type) {
                var indexes = picker.getIndexes();
                newValue = indexes[0] + data.minHour + ":" + (indexes[1] + data.minMinute);
            } else {
                var values = picker.getValues(), year = parseInteger(values[0]), month = parseInteger(values[1]), daysInMonth = getDaysInMonth(year, month), day = parseInteger(values[2]);
                "year-month" === data.type && (day = 1), day = daysInMonth < day ? daysInMonth : day;
                var hour = 0, minute = 0;
                "datetime" === data.type && (hour = parseInteger(values[3]), minute = parseInteger(values[4])), newValue = new Date(year, month - 1, day, hour, minute);
            }
            newValue = this.correctValue(newValue), this.updateColumnValue(newValue).then(function() {
                self.$emit("input", newValue), self.$emit("change", picker);
            });
        },
        updateColumnValue: function(value) {
            var self = this, columnValues = [], data = this.data, type = data.type, formatter = data.formatter, formatFunction = void 0 === formatter ? defaultFormatter : formatter, picker = this.getPicker();
            if ("time" === type) {
                var timeParts = value.split(":");
                columnValues = [ formatFunction("hour", timeParts[0]), formatFunction("minute", timeParts[1]) ];
            } else {
                var date = new Date(value);
                columnValues = [ formatFunction("year", "" + date.getFullYear()), formatFunction("month", padWithZeros(date.getMonth() + 1)) ], "date" === type && columnValues.push(formatFunction("day", padWithZeros(date.getDate()))), 
                "datetime" === type && columnValues.push(formatFunction("day", padWithZeros(date.getDate())), formatFunction("hour", padWithZeros(date.getHours())), formatFunction("minute", padWithZeros(date.getMinutes())));
            }
            return this.set({
                innerValue: value
            }).then(function() {
                return self.updateColumns();
            }).then(function() {
                return picker.setValues(columnValues);
            });
        }
    },
    created: function() {
        var self = this, correctedValue = this.correctValue(this.data.value);
        this.updateColumnValue(correctedValue).then(function() {
            self.$emit("input", correctedValue);
        });
    }
});